package ProjetoCompreJa.DAO;

import ProjetoCompreJa.ClassesBasicas.FuncionarioCJ;
import ProjetoCompreJa.Interfaces.InterfaceFuncionario_CJ;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author Marcos
 */
public class FuncionarioDAO_CJ implements InterfaceFuncionario_CJ {

    EntityManagerFactory emf;
    EntityManager em;

    public FuncionarioDAO_CJ() {
        emf = Persistence.createEntityManagerFactory("compreja");
        em = emf.createEntityManager();
    }

    @Override
    public void CadastrarFuncionario(FuncionarioCJ fun) throws Exception {
        try {
            em.getTransaction().begin();
            em.merge(fun);
            em.getTransaction().commit();
            emf.close();
        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
    }

    @Override
    public List<FuncionarioCJ> ListarFuncionarios() throws Exception {

        try {
            em.getTransaction().begin();
            Query consulta = em.createQuery("select funcionario from Funcionario funcionario");
            List<FuncionarioCJ> funcionarios = consulta.getResultList();
            em.getTransaction().commit();
            emf.close();
            return funcionarios;

        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
    }

    @Override
    public void ExcluirFuncionario(FuncionarioCJ fun) throws Exception {
        try {
            em.getTransaction().begin();
            em.remove(em.getReference(FuncionarioCJ.class, fun.getId()));
            em.getTransaction().commit();
            emf.close();
        } catch (Exception ex) {
            throw new Exception(ex.getMessage());

        }
    }

    @Override
    public void AlterarFuncionario(FuncionarioCJ fun) throws Exception {
        try {
            em.getTransaction().begin();
            em.merge(fun);
            em.getTransaction().commit();
            emf.close();
        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }

    }

    public List<FuncionarioCJ> VerificarLogin(FuncionarioCJ fun) throws Exception {
        try {
            em.getTransaction().begin();
            Query consulta = em.createQuery("select f from Funcionario f WHERE f.login = :login and f.senha = :senha");
            consulta.setParameter("login", fun.getLogin());
            consulta.setParameter("senha", fun.getSenha());
            List<FuncionarioCJ> funcionarios = consulta.getResultList();
            em.getTransaction().commit();
            emf.close();
            return funcionarios;

        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
    }

    public void AlterarLoginSenha(FuncionarioCJ fun) throws Exception {
        try {
            em.getTransaction().begin();
            Query query = em.createQuery("UPDATE Funcionario set login = :login, senha = :senha  WHERE cpf = :cpf");
            query.setParameter("login", fun.getLogin());
            query.setParameter("cpf", fun.getCpf());
            query.setParameter("senha", fun.getSenha());
            query.executeUpdate();
            em.getTransaction().commit();
            emf.close();
            /*em.getTransaction().begin();
             Query consulta = em.createQuery("UPDATE FuncionarioCJ set login = :login" +
             " and senha = :senha" +
             " where cpf = :cpf" );
             consulta.setParameter("login", fun.getLogin());
             consulta.setParameter("senha", fun.getSenha());
             consulta.setParameter("cpf", fun.getCpf());
             em.getTransaction().commit();
             emf.close();*/

        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
    }
}
